package com.lantosec.hmrp.core.util;

import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.modules.system.service.ISysDeptService;
import com.lantosec.hmrp.modules.system.service.ISysRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class LoginUtils {

    @Autowired
    private static ISysDeptService deptService;

    @Autowired
    private static ISysRoleService roleService;

    public static String getToken() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if (StringUtils.isNotEmpty(token)) {
            return token;
        } else {
            return null;
        }
    }

    /***
     * 获取用户名
     * @return
     */
    public static String getUserName(){
        String userName = TokenUtil.getUserName();
        return userName;
    }

    /***
     * 获取用户ID
     * @return
     */
    public static String getUserId(){
        String userId = TokenUtil.getUserId();
        return userId;
    }

    /***
     * 获取用户姓名
     * @return
     */
    public static String getName(){
        String name = TokenUtil.getName();
        return name;
    }

    /***
     * 获取权限列表
     * @return
     */
    public static List getPermission(){
        List<String> permissionList = roleService.getPermission(getUserId());
        return permissionList;
    }

    /***
     * 根据用户id获取所属部门列表
     * @return
     */
    public static String getDeptList(String userId){
        return deptService.getDeptList(userId);
    }

    /***
     * 根据用户id获取主部门
     * @return
     */
    public static String getMainDeptId(String userId){
        return deptService.getMainDeptId(userId);
    }
}

